#include "errors.h"
#include <errno.h>
#include <string>

// Error message stored in this string
std::string errormsg;

void set_error_message(const char * _msg) {
	errormsg = std::string(_msg);
}

void auto_set_error_message(int _errno) {
	if(_errno == EACCES) {
		errormsg = "Permission to create a socket \
				of the specified type and/or  protocol is denied.";
	}
	else if(_errno == EAFNOSUPPORT) {
		errormsg = "The  implementation  does not support the \
				specified address family.";
	}
	else if(_errno == EINVAL) {
		errormsg = "Unknown protocol, or protocol family not available.";
	}
	else if(_errno == EMFILE) {
		errormsg = "Process file table overflow.";
	}
	else if(_errno == ENFILE) {
		errormsg = "The system limit on the total number  of  open  \
				files  has  been reached.";
	}
	else if(_errno == ENOBUFS || _errno == ENOMEM) {
		errormsg = "Insufficient  memory is available.  The socket \
				cannot be created until sufficient resources are freed.";
	}
	else if(_errno == EPROTONOSUPPORT) {
		errormsg = "The protocol type or the specified  protocol  \
				is  not  supported within this domain.";
	}
	else {
		errormsg = "Unknown error.";
	}
}

const char * get_error_message() {
	return errormsg.c_str();
}
